﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace TPL.Xna.Common
{
    public class ImageSprite : Sprite
    {
        protected Texture2D Image;
        public Rectangle SourceRectangle;

        private Rectangle? _sourceRectangle;

        public ImageSprite(Texture2D image)
        {
            Image = image;
        }

        protected override void InternalUpdate(GameTime gameTime, Rectangle clientBounds)
        {
            _sourceRectangle = SourceRectangle == Rectangle.Empty ? (Rectangle?) null : SourceRectangle;

            if (HandleTap)
            {
                TapArea.Location = new Point((int)Position.X, (int)Position.Y);
                TapArea.Width = (int)(Image.Width * Scale);
                TapArea.Height = (int)(Image.Height * Scale);
            }

            base.InternalUpdate(gameTime, clientBounds);
        }

        protected override void InternalDraw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            //Draw the sprite
            spriteBatch.Draw(Image, Position, _sourceRectangle, Color, Angle, Origin, Scale, Effect, ZIndex);
        }
    }

    public class BackgroundSprite : ImageSprite
    {
        public BackgroundSprite(Texture2D background)
            : base(background)
        {
            Position = Vector2.Zero;
            SourceRectangle = new Rectangle(0, 0, 0, 0);
            ZIndex = 0f;
        }

        protected override void InternalUpdate(GameTime gameTime, Rectangle clientBounds)
        {
            SourceRectangle.Width = clientBounds.Height;
            SourceRectangle.Height = clientBounds.Width;
            base.InternalUpdate(gameTime, clientBounds);
        }
    }
}